FROM openfaas/of-watchdog:0.7.2 as watchdog
FROM python:3.7.7-slim-buster

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog

ARG ADDITIONAL_PACKAGE
RUN set -ex; \
	\
	apt-get update; \
	apt-get install -y --no-install-recommends musl-dev gcc make ${ADDITIONAL_PACKAGE}; \
	groupadd -r -g 999 app; \
	useradd -r -g app -u 999 app; \
    chmod +x /usr/bin/fwatchdog; \
    mkdir -p /home/app; \
    chown app /home/app;

USER app

ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

COPY index.py           .
COPY requirements.txt   .
USER root
RUN pip install -r requirements.txt
USER app

RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
COPY function/requirements.txt	.
RUN pip install --user -r requirements.txt

WORKDIR /home/app/

USER root
COPY function   function
RUN chown -R app:app ./
USER app

ENV fprocess="python index.py"

ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:5000"

HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
